package jxta.chat.entities;

import jxta.chat.util.Constants;
import jxta.chat.util.Constants.MessageTypes;
import net.jxta.endpoint.Message;
import net.jxta.endpoint.MessageElement;

public class MessageFactory {
	
	private static MessageFactory mInstance = null;
	
	private MessageFactory(){
	}
	
	public static MessageFactory getInstance(){
		if (mInstance == null){
			mInstance = new MessageFactory();
		}
		
		return mInstance;
	}
	
	/**
	 * Returns a relevant class that handles the given message type 
	 */
	public AbstractMessage getMessageClass(MessageTypes messageType){
		
		AbstractMessage retVal = null;
		
		if (messageType.equals(Constants.MessageTypes.LOGIN)){
			retVal = new LoginMessage();
		}
		else if (messageType.equals(Constants.MessageTypes.REGULAR)){
			retVal = new RegularMessage();
		}
		else if (messageType.equals(MessageTypes.LOGOUT)){
			retVal = new LogOutMessage();
		}
		else if (messageType.equals(MessageTypes.VOIP_CHAT)){
			retVal = new VoiceChatMessage();
		}
		else if (messageType.equals(MessageTypes.USER_DISCOVERED)){
			retVal = new UserDiscoveredMessage();
		}
		
		return retVal;
	}
	
	public AbstractMessage getMessageClass(Message msg){
		
		AbstractMessage retVal = null;
		
		MessageElement type = msg.getMessageElement(Constants.MESSAGE_TYPE_SPACE);
		
		if (type != null){
		
			if (type.toString().equals(Constants.MessageTypes.LOGIN.toString())){
				retVal = new LoginMessage(msg);
			}
			else if (type.toString().equals(Constants.MessageTypes.REGULAR.toString())){
				retVal = new RegularMessage(msg);
			}
			else if (type.toString().equals(MessageTypes.VOIP_CHAT.toString())){
				retVal = new VoiceChatMessage(msg);
			}
			else if (type.equals(MessageTypes.USER_DISCOVERED.toString())){
				retVal = new UserDiscoveredMessage(msg);
			}
		}
		
		return retVal;
	}
}
